package com.yonyou.struts2demo.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author wwj
 * @Date 2021/12/13
 * @Description
 */
public class LoginInterceptor extends MethodFilterInterceptor {

    // 这个方法里面写拦截器逻辑
    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {

        // 判断session 里面是否有名称是 username 的值
        HttpServletRequest request = ServletActionContext.getRequest();
        Object obj = request.getSession().getAttribute("username");
        // 判断
        if (obj != null) {
            // 说明是登录状态,执行相应方法
            return invocation.invoke();
        } else {
            // 不是登录状态
            return "login";
        }
    }
}
